package com.zyd.blog.personal.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 地址处理工具类
 * @author mfy
 */
public class StringUtils {

    private static String addressTip = "&";

    private static String strTip = "#";

    /**
     * 字符串拼接
     * @param source    原始字符串
     * @param addStr    添加字符串
     * @param defaultString 默认添加
     * @param betweenStr    中间分隔符
     * @return  拼接后的字符串
     */
    public static String combinationStr(String source,String addStr,String defaultString,String betweenStr){
        if(addStr == null || "".equals(addStr)){
            source = source + defaultString + betweenStr;
        }else{
            source = source + addStr + betweenStr;
        }
        return source;
    }

    /**
     * 地址组合
     * @param country   国家
     * @param province  省份
     * @param city  城市
     * @param area  地区
     * @param detailed  详细描述
     * @return  地址字符串
     */
    public static String combinationAddress(String country,String province,String city,String area,String detailed){
        String address = "";

        address = StringUtils.combinationStr(address,country,"中国",addressTip);
        address = StringUtils.combinationStr(address,province," ",addressTip);
        address = StringUtils.combinationStr(address,city," ",addressTip);
        address = StringUtils.combinationStr(address,area," ",addressTip);
        address = StringUtils.combinationStr(address,detailed," ",addressTip);

        return  address;
    }

    /**
     * 将地址字符串拆成地址类型
     * @param str   地址字符串
     * @return  地址map
     */
    public static Map<String,Object> splitAddress(String str){
        if (str == null || "".equals(str)) {
            return new HashMap<>(0);
        }
        Map<String,Object> address = new HashMap<>(16);
        String[] addressStr = str.split(addressTip);
        address.put("country",addressStr[0]);
        address.put("province",addressStr[1]);
        address.put("city",addressStr[2]);
        address.put("Area",addressStr[3]);
        address.put("detailed",addressStr[4]);
        return address;
    }

    /**
     * 得到无连接符的地址
     * @param str   有连接符的地址
     * @return  无连接符的地址
     */
    public static String getAddressOfStr(String str){
        String[] addressStr = str.split(addressTip);
        StringBuilder address = new StringBuilder();
        for (String item : addressStr) {
            if(item == null || "".equals(item.trim())){
                continue;
            }
            address.append(item);
        }
        return address.toString();
    }

    /**
     * 将字符串数组拼接为长字符串
     * @param array  字符串数组
     * @return  长字符串
     */
    public static String combinationString(String [] array){
        StringBuilder str = new StringBuilder();
        for (String item : array){
            str.append(item).append(strTip);
        }
        return str.toString();
    }

    /**
     * 将长字符串拆分为字符串数组
     * @param str   长字符串
     * @return  字符串数组
     */
    public static String[] splitStr(String str){
        if(str == null || "".equals(str)){
            return null;
        }
        return str.split(strTip);
    }

    /**
     * 得到无连接字符的字符串
     * @param str   带连接符的字符串
     * @return  无连接符的字符串
     */
    public static String getStringOfSplit(String str){
        String[] strs = StringUtils.splitStr(str);
        StringBuilder result = new StringBuilder();
        for (String item : strs) {
            if(item == null || "".equals(item.trim())){
                continue;
            }
            result.append(item).append(" ");
        }
        return result.toString();
    }

    /**
     * 通过完整地址字符串返回地址中的省和市
     * @param address   完整地址字符串
     * @return  地址中的省市
     */
    public static String getAddressOnlyProvinceAndCity(String address){
        Map<String,Object> addressMap = StringUtils.splitAddress(address);
        return addressMap.get("province") + " " + addressMap.get("city");
    }

    /**
     * 得到地址市
     * @param address   完整地址字符串
     * @return  得到地址中的市
     */
    public static String getAddressOnlyCity(String address){
        Map<String,Object> addressMap = StringUtils.splitAddress(address);
        return  (String)addressMap.get("city");
    }

    /**
     * 解析json字符串并拼接其中内容
     * @param json  json字符串
     * @param getContentOfKey  需要得到的内容key
     * @return  拼接后字符创
     */
    public static String  parsingJsonArray(String json,String getContentOfKey){
        JSONArray jsonObject = (JSONArray) JSONObject.parse(json);
        StringBuilder result = new StringBuilder();
        for(Object jsonObjects : jsonObject){
            String item = (String) ((Map<String, Object>) jsonObjects).get(getContentOfKey);
            result.append(item).append(" ");
        }
        return result.toString();
    }


}
